﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Admin.Application.Custom.Fins.Dto;
using Magicodes.Admin.EntityFrameworkCore;
using System.Linq;
using System.Linq.Dynamic;
using System.Linq.Expressions;
using Abp.Linq.Extensions;
using Abp.AutoMapper;
using Microsoft.EntityFrameworkCore;
using Abp.Application.Services.Dto;
using System.Linq.Dynamic.Core;
using Magicodes.Admin.Custom.Fins;
using Magicodes.Admin;
using Abp.EntityFrameworkCore;

namespace Admin.Application.Custom.Fins
{
    public class MonthlyListingAppService : AppServiceBase, IMonthlyListingAppService
    {  
        private readonly IMonthlyListingRepository _monthlyListingRepository;
        private readonly IMonthlyListingManager _monthlyListingManager;
        public MonthlyListingAppService(IMonthlyListingManager monthlyListingManager, IMonthlyListingRepository monthlyListingRepository)
        {
            _monthlyListingRepository = monthlyListingRepository;

            _monthlyListingManager = monthlyListingManager;
        }
        public async Task<PagedResultDto<MonthlyListingListDto>> GetPageList(GetPageListInput input)
        { 
            var query = _monthlyListingRepository.GetAll()
                .WhereIf(input.Year > 2018, s => s.SettlementDate.Year == input.Year)
                .WhereIf(input.Month > 0, s => s.SettlementDate.Month == input.Month);

                 var count = await query.CountAsync();

            if (count == 0)
                return new PagedResultDto<MonthlyListingListDto>();
            var list = query
              .OrderBy(input.Sorting)
              .PageBy(input)
              .ToList();

            var reList = list.MapTo<List< MonthlyListingListDto>>();

            return new PagedResultDto<MonthlyListingListDto>(
                count,
                reList
                );
        }

        public   Task ImportFromExcel(List<ImportMonthlyListingDto> importMonthlyListingDtos)
        {
            var list = importMonthlyListingDtos.MapTo<List<MonthlyListing>>();
            _monthlyListingRepository.ImportFromExcel(list);

            return Task.CompletedTask;
        }
    }
}
